package com.student.wym.service.impl;

import com.student.wym.mapper.AnswerMapper;
import com.student.wym.mapper.StudentMapper;
import com.student.wym.po.Answer;
import com.student.wym.po.Student;
import com.student.wym.service.IAnswerService;
import com.student.wym.vo.AnswerRequestVo;
import com.student.wym.vo.AnswerRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public AnswerRespVo findOneByKey(AnswerRequestVo reqVo) {
        String keyWord = reqVo.getKeyWord();
        List<Answer> answers = new ArrayList<Answer>();
        Student student = new Student();
        if (StringUtils.isNotBlank(keyWord)) {
            switch (keyWord) {
                case "studentNo":
                    Integer studentId = studentMapper.findByStudentNo(reqVo.getKeyValue()); //根据学号查ID
                    if (studentId != null) {
                        answers = answerMapper.findByStudentId(studentId);
                    }
                    break;
                case "name":
                    student = studentMapper.findByName(reqVo.getKeyValue());
                    if (null != student) {
                        answers = answerMapper.findByStudentId(student.getId());
                    }
                    break;
                default:
                    break;
            }
        }
        AnswerRespVo resultVo = new AnswerRespVo();
        resultVo.setAnswers(answers);
        return resultVo;
    }

    @Override
    public Student findStudentByNo(String studentNo) {
        return studentMapper.findOneByNo(studentNo);
    }

    @Override
    public List<Student> findStudentByName(String studentName) {
        List<Student> students = studentMapper.findOneByName(studentName);
        return students;
    }

}
